<?php

namespace app\stores\controller\miniapp\promotion;

use app\BaseController;
use app\beautiful\model\mall\GoodsModel;
use app\beautiful\model\member\MemberModel;
use app\common\model\DataConfigModel;
use app\stores\model\promotion\ActivityModel;
use app\stores\model\promotion\PromotionmemberModel;
use app\stores\model\promotion\PromotionmoneydetailModel;
use app\stores\model\promotion\PromotionrecordModel;
use app\stores\model\promotion\TeamActivityModel;
use app\stores\model\promotion\TeamModel;
use app\stores\model\withdraw\MemberWithdrawAccountModel;
use app\stores\model\withdraw\MemberWithdrawModel;
use think\facade\Db;


class Index extends BaseController{

    /*获取已加入团队列表*/
    public function teamList(){
        $member_id = $this->request->memberId;
        $return = PromotionmemberModel::list(['member_id'=>$member_id,'is_effect'=>1,'is_delete'=>0],['id'=>'asc'],1,9999);
        return $this->success($return);
    }
    /*添加团队成员*/
    public function member_add(){
        $member_id=$this->request->param('member_id',null);
        $team_id=$this->request->param('team_id',null);
        $username=$this->request->param('username',null);
        $mobile=$this->request->param('mobile',null);
        $sex=$this->request->param('sex',null);
        $TeamModel= new TeamModel();
        $s=[];
        $s[]=['id','=',$team_id];
        $s[]=['is_delete','=',0];
        $all_info=$TeamModel->where($s)->find();
        if(empty($all_info)) return $this->error('团队不存在');
        $s=[];
//        $s[]=['team_id','=',$team_id];
        $s[]=['member_id','=',$member_id];
        $s[]=['is_delete','=',0];
        $s[]=['type','=',1];
        $m_info=PromotionmemberModel::where($s)->find();
        if(!empty($m_info)) return $this->error('改用户已加入团队');
//        $member_info=MemberModel::where(['member_id'=>$member_id])->find();
        $p_insert['add_time']=date('Y-m-d H:i:s');
        $p_insert['edit_time']=date('Y-m-d H:i:s');
        $p_insert['member_id']=$member_id;
        $p_insert['type']=1;
        $p_insert['name']=$username;
        $p_insert['sex']=empty($sex)?0:$sex;
        $p_insert['mobile']=$mobile;
        $p_insert['team_id']=$team_id;
        $insert_id=PromotionmemberModel::insertGetId($p_insert);
        return $this->success($insert_id);
    }
    /*查看团队成员*/
    public function member_list(){
        $page=$this->request->page;
        $limit=$this->request->limit;
        $team_id=$this->request->param('team_id',null);
        $TeamModel= new TeamModel();
        $s=[];
        $s[]=['id','=',$team_id];
        $s[]=['is_delete','=',0];
        $all_info=$TeamModel->where($s)->find();
        if(empty($all_info)) return $this->error('团队不存在');
        $where['team_id'] = $team_id;
        $where['type'] = 1;
        $order = ['a.id'=>'desc'];
        $return = PromotionmemberModel::list($where,$order,$page,$limit);
        if(!empty($return['list'])){
            foreach ($return['list'] as &$list){
                $s=[];
                $s[]=['member_id','=',$list['member_id']];
                $s[]=['team_id','=',$list['team_id']];
                $statis=PromotionmoneydetailModel::data_statis($s,[],$team_id);
                $list['all_num']=$statis['all_num'];
                $list['all_money']=$statis['all_money'];
                $list['today_money']=$statis['today_money'];
                $list['today_num']=$statis['today_num'];
                $lead_member = PromotionmemberModel::where(['is_effect'=>1,'is_delete'=>0,'member_id'=>$list['member_id'],'type'=>2,'team_id'=>$list['team_id']])->find();
                $list['type'] = empty($lead_member)?1:2;
            }
        }
        return $this->success($return);
    }
    /*用户提现操作*/
    public function withdraw(){
        $type = $this->request->type;
        $team_id = $this->request->teamId;
        $member_id = $this->request->memberId;
        $money=$this->request->param('money/f',0);

        if($money<=0) return $this->error('提现金额大于0元');

        $member_id = $type==2?0:$member_id;
        $record = PromotionrecordModel::info(['member_id'=>$member_id,'team_id'=>$team_id]);
        if($record['money']<$money) return $this->error('提现金额不足');

        if($type==2){
            $where [] =['team_id','=',$team_id];
            $member_id = 0;
        }
        $where [] =['member_id','=',$member_id];
        $where [] =['is_delete','=',0];
        $withdraw_account = MemberWithdrawAccountModel::info($where);
        if(empty($withdraw_account)) return $this->error('提现账户不存在');

        //用户提现手续费
        $dataconfig=new DataConfigModel();
        $info=$dataconfig->where(['id'=>1])->find();

        Db::startTrans();
        try{
            $m_w = new MemberWithdrawModel();
            $m_w->member_id = $member_id;
            $m_w->team_id = $team_id;
            $m_w->withdraw_account_id = $withdraw_account['withdraw_account_id'];
            $m_w->money = $money;
            $m_w->before_money = $record['money'];
            $m_w->after_money = $record['money'] - $money;
            $m_w->fee_ratio = $info['user_money_ratio'];
            $m_w->fee = ($info['user_money_ratio']/100) * $money;
            $m_w->type = 1;
            $m_w->save();

            PromotionrecordModel::up(['money'=>$record['money'] - $money],['id'=>$record['id']]);

            $p_m_de = new PromotionmoneydetailModel();
            $p_m_de->member_id = $member_id;
            $p_m_de->team_id = $team_id;
            $p_m_de->withdraw_id = $m_w->withdraw_id;
            $p_m_de->type = -1;
            $p_m_de->status = 0;
            $p_m_de->money = $money;
            $p_m_de->old_money = $record['money'];
            $p_m_de->new_money = $record['money'] - $money;
            $p_m_de->save();

            Db::commit();
        } catch (\Exception $e){
            Db::rollback();
            return $this->error($e->getMessage());
        }

        return $this->success('提交成功');

    }

    /*删除团队成员*/
    public function member_del(){
        $member_id=$this->request->param('member_id',null);
        $team_id=$this->request->param('team_id',null);
//        $TeamModel= new TeamModel();
//        $s=[];
//        $s[]=['id','=',$team_id];
//        $s[]=['is_delete','=',0];
//        $all_info=$TeamModel->where($s)->find();
//        if(empty($all_info)) return $this->error('团队不存在');
        $s=[];
        $s[]=['team_id','=',$team_id];
        $s[]=['member_id','=',$member_id];
        $s[]=['is_delete','=',0];
        $s[]=['type','=',1];
        $m_info=PromotionmemberModel::where($s)->find();
        if(empty($m_info)) return $this->error('团员不存在');
        $up=[];
        $up['is_delete']=1;
        $insert_id=PromotionmemberModel::up($up,$s);
        return $this->success($insert_id);
    }
}

